package com.atguigu.tingshu.common.util;

import java.io.IOException;
import java.lang.ref.SoftReference;
import java.lang.ref.WeakReference;

/**
 * 获取当前用户信息帮助类
 */
public class AuthContextHolder {

    private static ThreadLocal<Long> userId = new ThreadLocal<Long>();
    private static ThreadLocal<String> username = new ThreadLocal<String>();

    public static void setUserId(Long _userId) {
        userId.set(_userId);
    }

    public static Long getUserId() {
        //未完成登录功能前，暂时固定为1
        //return 1L;
        //完成登录及验证后，改为动态从ThreadLocal获取
        return userId.get();
    }

    public static void removeUserId() {
        userId.remove();
    }

    public static void setUsername(String _username) {
        username.set(_username);
    }

    public static String getUsername() {
        return username.get();
    }

    public static void removeUsername() {
        username.remove();
    }


    /**
     * -Xms10m -Xmx10m
     * @param args
     * @throws IOException
     */
    public static void main(String[] args) throws IOException {
        //申明软引用
        //SoftReference<byte[]> softReference = new SoftReference<>(new byte[1024*1024*7]);
        //System.out.println("gc前" + softReference.get());
        //
        ////手动 GC
        //System.gc();
        //System.in.read();  //阻塞主进程
        //
        //System.out.println("gc后" + softReference.get());


        WeakReference<byte[]> weakReference = new WeakReference<>(new byte[1024]);
        System.out.println("gc前" + weakReference.get());
        System.gc();
        System.out.println("gc后" + weakReference.get());

    }
}
